<?php
class MEmail extends MY_Model {

    //状态
	const STATE_ENABLED = 1;
	const STATE_DISABLED = 2;
	
	//排序方式
	const ORDER_ID_DESC = 1;
	const ORDER_ID_ASC = 2;
	const ORDER_EMAIL_TIMES_DESC = 3;
	const ORDER_EMAIL_TIMES_ASC = 4;
	const ORDER_EMAIL_LAST_TIME_DESC = 5;
	const ORDER_EMAIL_LAST_TIME_ASC = 6;
	const ORDER_RMARK_LENGTH_GT_ZERO = 7;
	protected $table = 'emails';
	private $filter = array();

    function __construct() {
        parent::__construct();
    }
    
    public static function getState($state=NULL){
        $define = array(self::STATE_ENABLED=>'可用',self::STATE_DISABLED=>'失效');
    	if($state === NULL){
    		return $define;
    	}else{
    		return key_exists($state, $define)?$define[$state]:'';
    	}
    }
    
    public static function getOrderType($orderType=NULL){
        $define = array(
            self::ORDER_ID_DESC=>'ID倒序',
            self::ORDER_ID_ASC=>'ID升序',
            self::ORDER_EMAIL_TIMES_DESC=>'email次数倒序',
            self::ORDER_EMAIL_TIMES_ASC=>'email次数升序',
            self::ORDER_EMAIL_LAST_TIME_DESC=>'上次email时间倒序',
            self::ORDER_EMAIL_LAST_TIME_ASC=>'上次email时间升序',
            self::ORDER_RMARK_LENGTH_GT_ZERO=>'写有备注信息在前'
            
        );
        if($orderType === NULL){
            return $define;
        }else{
            return key_exists($state, $define)?$define[$state]:'';
        }
    }
    
	public function getList($limit, $sort=false){
		$this->db->select( $this->table.'.*');
		$this->db->distinct();
		$this->db->from($this->table);
		$this->db->limit($limit['limit'],$limit['offset']);
		$this->formfilter->doFilter();
		if($this->_user->role_id == 3){ //测评组的，只显示自己的
		    $this->db->where('creator_id',$this->_user->id);
		}
		if($sort){
		    switch ($sort){
		        case self::ORDER_ID_DESC:
		            $this->db->order_by('id', 'desc');
		            break;
		        case self::ORDER_ID_ASC:
		            $this->db->order_by('id', 'asc');
		            break;
		        case self::ORDER_EMAIL_TIMES_DESC:
		            $this->db->order_by('email_times', 'desc');
		            break;
		        case self::ORDER_EMAIL_TIMES_ASC:
		            $this->db->order_by('email_times', 'asc');
		            break;
		        case self::ORDER_EMAIL_LAST_TIME_DESC:
		            $this->db->order_by('email_last_time', 'desc');
		            break;
		        case self::ORDER_EMAIL_LAST_TIME_ASC:
		            $this->db->order_by('email_last_time', 'asc');
		            break;
		        case self::ORDER_RMARK_LENGTH_GT_ZERO:
		            $this->db->order_by('remark', 'desc');
		            break;
		    }
		}else{
			$this->db->order_by('id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	public function getCount(){
		$this->db->select('count(id) as num');
		$this->db->from($this->table);
		$this->formfilter->doFilter();
		if($this->_user->role_id == 3){ //测评组的，只显示自己的
		    $this->db->where('creator_id',$this->_user->id);
		}
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	//得到全部数量
	public function getTotalCount(){
	    $this->db->select('count(id) as num');
	    $this->db->from($this->table);
	    $tmp = $this->db->get()->result();
	    return (int)$tmp[0]->num;
	}
	
	public function getById($id){
		$query = $this->db->where('id',$id)->get($this->table);
		return $query->row();
	}
	
	public function updateEmailsNum($emailIdArr){
	    $this->db->set('email_times','email_times+1',false);
	    $this->db->set('email_last_time',time());
	    $this->db->where_in('id',$emailIdArr);
	    return $this->db->update($this->table);
	}
	
}
